<?php

/* 接收用户登录表单*/
include '../global.php';
include LIB_PATH.'bean/User.php';
include LIB_PATH.'biz/UserValidate.php';
include LIB_PATH.'utils/functions.php';
include LIB_PATH.'db/db.php';

session_start();

/* 检查用户登录 */
$userStr = getPostValue('username'); 
$passwdStr = getPostValue('password'); 

if( $userStr !=null && $passwdStr!=null ){
    $user = new User($userStr, $passwdStr);
    $userValidate = new UserValidate($user);
    
    if($userValidate->validate($GLOBALS['DB'])){
        /* 用户登录成功 */
        $_SESSION['access'] = true;
        $_SESSION['username'] = $user->name;
        header('Location: ../../public_files/mainPage.php');
    }else{
        // 不存在的用户或者密码
        $_SESSION['access'] = false;
        $_SESSION['username'] = "";
        header("Location: ../../public_files/errorPage/401.php");
    }
    
}else{
    // 没有填写用户名或密
    $_SESSION['access'] = false;
    $_SESSION['username'] = null;
    header("Location: ../../public_files/errorPage/401.php");
}